package com.his.controller;

import com.his.annotation.RequireRoles;
import com.his.common.Const;
import com.his.common.PageParam;
import com.his.common.Rsp;
import com.his.entity.Patient;
import com.his.entity.Recipel;
import com.his.service.RecipelService;
import com.his.utils.MyUtil;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (Recipel)表控制层
 *
 * @author makejava
 * @since 2020-10-23 18:14:30
 */
@RestController
@RequestMapping("recipel")
public class RecipelController {
    /**
     * 服务对象
     */
    @Resource
    private RecipelService recipelService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @PostMapping("selectOne")
    public Rsp selectOne(Integer id) {
        return this.recipelService.queryById(id);
    }


    /**
     * 增加处方
     */
    //@RequireRoles(roles = "medicinal",index = 0)
    @PostMapping("insertOne")
    @RequireRoles(roles = {"doctor"}, index = 0)
    public Rsp insertOne(@RequestHeader(Const.TOKEN) String token, @RequestBody Recipel recipel) {
        int userByToken = 0;
        try {
            userByToken = MyUtil.getUserByToken(token);
        } catch (Exception e) {
            e.printStackTrace();
        }
        recipel.setDoctorId(userByToken);
        return this.recipelService.insert(recipel);
    }

    /*
     *删除处方
     */
    @PostMapping("deleteOne")
    public Rsp deleteByIdLogic(@RequestBody Recipel recipel) {
        return recipelService.deleteByIdLogic(recipel.getId());
    }

    /**
     * 修改处方
     */
    @PostMapping("updateOne")
    public Rsp updateOne(@RequestBody Recipel recipel) {
        return recipelService.update(recipel);
    }

    /**
     * 查询处方列表
     */
    @RequireRoles(roles = {"nurse", "doctor"}, index = 0)
    @PostMapping("searchSomeRecipelPage")
    public Rsp searchSomeRecipelPage(@RequestHeader(Const.TOKEN) String token, @RequestBody PageParam<Recipel> pageParam) throws Exception {
        return Rsp.success(recipelService.searchSomeRecipelPage(pageParam, MyUtil.getUser(token)));
    }

    /**
     * 护士执行医嘱
     *
     * @param token
     * @param pageParam
     * @return
     */
    @RequireRoles(roles = {"nurse"}, index = 0)
    @PostMapping("getPatientRecipelInfo")
    public Rsp getPatientRecipelInfo(@RequestHeader(Const.TOKEN) String token, @RequestBody PageParam<Patient> pageParam) {
        return Rsp.success(recipelService.getPatientRecipelInfo(pageParam));
    }

    /**
     * 护士核对处方
     * status改为1
     */
    @PostMapping("recipelSure")
    public Rsp recipelSure(@RequestBody Recipel recipel) {
        return recipelService.recipelSure(recipel);
    }

    /**
     * 药房确认拿药
     * status改为2
     */
    @RequireRoles(roles = "medicinal", index = 0)
    @PostMapping("medicinalSure")
    public Rsp MedicinalSure(@RequestHeader(Const.TOKEN) String token, @RequestBody Recipel recipel) {
        return recipelService.medicinalSure(token, recipel);
    }

    /**
     * 药房查药("medSearchSomeRecipelPage")
     */
    @PostMapping("medSearchSomeRecipelPage")
    public Rsp medSearchSomeRecipelPage(@RequestBody PageParam<Recipel> pageParam) {
        return Rsp.success(recipelService.medSearchSomeRecipelPage(pageParam));
    }

    /**
     * 医生申请退药
     *
     * @param recipel
     * @return
     */
    @PostMapping("drawbackMedicinal")
    public Rsp drawbackMedicinal(@Validated @RequestBody Recipel recipel) {
        return recipelService.drawbackMedicinal(recipel);
    }
}
